package com.shangyueshang.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.shangyueshang.model.GoodsRevenue;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * @author: JokeyZheng
 * @email: zhengjingfeng@ruqimobility.com
 * @created: 2022/1/17 19:04
 * @version: v1.0.0
 */
@Component
public interface GoodsRevenueMapper extends BaseMapper<GoodsRevenue> {
	
	/**
	 * 根据SKU查询收入
	 *
	 * @param params
	 * @return
	 */
	@Select("<script>" +
			"   SELECT " +
			"       shop, " +
			"       sku, " +
			"       DATE_FORMAT(trade_date, '%Y-%m') AS tradeMonth, " +
			"       SUM( product_sales ) AS product_sales, " +
			"       SUM( sale_revenue ) AS sale_revenue, " +
			"       SUM( sale_quantity ) AS sale_quantity, " +
			"       SUM( selling_fee ) AS selling_fee, " +
			"       SUM( fba_fee ) AS fba_fee, " +
			"       SUM( headway_fee ) AS headway_fee, " +
			"       SUM( purchase_fee ) AS purchase_fee, " +
			"       SUM( pack_fee ) AS pack_fee, " +
			"       SUM( return_fee ) AS return_fee, " +
			"       SUM( return_quantity ) AS return_quantity " +
			"   FROM  " +
			"       goods_revenue " +
			"	<where> " +
			"       <if test='params.shop != null'> " +
			"		 AND shop IN " +
			"			<foreach collection='params.shop' item='item' open='(' separator=',' close=')'>" +
			"				#{item} " +
			"			</foreach>" +
			"       </if> " +
			"		<if test='params.sku != null'> " +
			"			AND sku = #{params.sku} " +
			"		</if> " +
			"		<if test='params.goods_type != null'> " +
			"			AND goods_type = #{params.goods_type} " +
			"		</if> " +
			"		<if test='params.from_trade_date != null'> " +
			"			AND trade_date <![CDATA[ >= ]]> #{params.from_trade_date} " +
			"		</if> " +
			"		<if test='params.to_trade_date != null'> " +
			"			AND trade_date <![CDATA[ <= ]]> #{params.to_trade_date} " +
			"		</if> " +
			"	</where> " +
			"   GROUP BY shop, sku, tradeMonth " +
			"</script>")
	List<GoodsRevenue> countSales(@Param("params") Map<String, Object> params);
	
}
